package cn.tedu.straw.portal.controller;

import cn.tedu.straw.portal.ex.*;
import cn.tedu.straw.portal.util.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler({ServiceException.class,  FileUploadException.class})
    public Result handlerException(Throwable e){
        if (e instanceof InviteCodeException) {
           return Result.fail(Result.State.ERR_INVITE_CODE,e);
        } else if (e instanceof ClassDisabledException) {
            return Result.fail(Result.State.ERR_CLASS_DISABLED,e);
        } else if (e instanceof PhoneDuplicateException) {
           return Result.fail(Result.State.ERR_PHONE_DUPLICATE,e);
        } else if (e instanceof InsertException) {
            return Result.fail(Result.State.ERR_INSERT,e);
        }else if(e instanceof ParamValidationException) {
          return Result.fail(Result.State.ERR_PARAM_VALIDATION,e);
        } else if(e instanceof FileEmptyException){
            return Result.fail(Result.State.ERR_FILE_UPLOAD_EMPTY,e);
        }else if(e instanceof FileSizeException){
            return Result.fail(Result.State.ERR_FILE_UPLOAD_SIZE,e);
        }else if(e instanceof FileTypeException){
            return Result.fail(Result.State.ERR_FILE_UPLOAD_TYPE,e);
        }else if(e instanceof FileUploadException){
            return Result.fail(Result.State.ERR_FILE_UPLOAD_IO,e);
        }else if(e instanceof FileStateException){
            return Result.fail(Result.State.ERR_FILE_UPLOAD_STATE,e);
        }else if(e instanceof QuestionNotFoundException){
            return Result.fail(Result.State.ERR_QUESTION_NOT_FOUND,e);
        }else if(e instanceof AnswerNotFoundException){
            return Result.fail(Result.State.ERR_ANSWER_NOT_FOUND,e);
        }
        return null;
    }
}
